package com.zyl.pagergallerymoredemo_java.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zyl.pagergallerymoredemo_java.R;
import com.zyl.pagergallerymoredemo_java.adapter.GalleryAdapter;
import com.zyl.pagergallerymoredemo_java.pojo.Pixabay;
import com.zyl.pagergallerymoredemo_java.viewModel.GalleryViewModel;

import java.util.List;

public class GalleryFragment extends Fragment {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private GalleryViewModel galleryViewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_gallery, container, false);
        swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        recyclerView = view.findViewById(R.id.recyclerView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        galleryViewModel = new ViewModelProvider(this,
                new ViewModelProvider.AndroidViewModelFactory(requireActivity().getApplication()))
                .get(GalleryViewModel.class);

        final GalleryAdapter galleryAdapter = new GalleryAdapter(galleryViewModel);

        recyclerView.setAdapter(galleryAdapter);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        galleryViewModel.getListLiveData().observe(this, new Observer<List<Pixabay.PhotoItem>>() {
            @Override
            public void onChanged(List<Pixabay.PhotoItem> photoItems) {
                if (galleryViewModel.needToScrollToTop) {
                    recyclerView.scrollToPosition(0);
                    galleryViewModel.needToScrollToTop = false;
                }
                galleryAdapter.submitList(photoItems);
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        if (galleryViewModel.getListLiveData().getValue() == null) {
            galleryViewModel.fetchData();
        }

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                galleryViewModel.resetQuery();
            }
        });

        galleryViewModel.getStatusLiveData().observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                galleryAdapter.footerViewStatus = integer;
                galleryAdapter.notifyItemChanged(galleryAdapter.getItemCount() - 1);
                if (integer == GalleryViewModel.DATA_STATUS_NO_MODE) {//网络错误
                    swipeRefreshLayout.setRefreshing(false);
                }
            }
        });

        //recycler滚动监听
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy < 0) {
                    Log.d("myTAG", "onScrolled: 往上划");
                    return;
                } //说明是往上划

                StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
                int[] ints = new int[2];
                layoutManager.findLastVisibleItemPositions(ints);
                if (ints[0] == galleryAdapter.getItemCount() - 1) {
                    galleryViewModel.fetchData();
                }
            }
        });


    }
}